home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 047a / lex_yacc.arj / TABLE.L < prev    next >
Text File  |  1989-05-28  |  2KB  |  89 lines

  1.  
  2.   { TABLE.L: Lex demonstration program for the use of character tables;
  3.     accepts identifiers, signed integers, whitespace and newline as input
  4.     and prints out the names of the matched tokens; unrecognized characters
  5.     are converted to and echoed as '?' (ASCII 63). }
  6.  
  7.   uses LexLib;
  8.  
  9.   { redefined input functions that remap characters: }
  10.   function input : char; forward;
  11.   procedure unput(c : char); forward;
  12.  
  13. %T
  14. 1    Aa
  15. 2    Bb
  16. 3    Cc
  17.  { ... }
  18. 26    Zz
  19. 27    \ \t
  20. 28    +
  21. 29    -
  22. 30    0
  23. 31    1
  24.  { ... }
  25. 39    9
  26. 40    \n
  27. %T
  28.  
  29. letter        [A-Z]
  30. digit        [0-9]
  31.  
  32. %%
  33.  
  34. {letter}({letter}|{digit})*    write('<identifier> ');
  35. [+-]?{digit}+            write('<number> ');
  36. " "+                write('<space> ');
  37. \n                writeln('<newline>');
  38. .                               write(yytext[1]);
  39.  
  40. %%
  41.  
  42. (* implementation of input functions and main program: *)
  43.  
  44. var buf : string; (* lookahead buffer *)
  45. function input : char;
  46.   var c : char;
  47.   begin
  48.     if length(buf)>0 then
  49.       (* get char from lookahead buffer: *)
  50.       begin
  51.         input := buf[1];
  52.         delete(buf, 1, 1)
  53.       end
  54.     else
  55.       begin
  56.         (* get character from input file (converting line end <CR><LF>
  57.            to newline, and handling end of file): *)
  58.         if eof(yyin) then
  59.           c := #0
  60.         else if eoln(yyin) then
  61.           begin
  62.             c := #10; (* newline *)
  63.             readln(yyin)
  64.           end
  65.         else
  66.           read(yyin, c);
  67.         (* remap characters: *)
  68.         case c of
  69.           #0: input := #0;
  70.           'a'..'z': input := chr(succ(ord(c)-ord('a')));
  71.           'A'..'Z': input := chr(succ(ord(c)-ord('A')));
  72.           ' ', #9: input := #27;
  73.           '+': input := #28;
  74.           '-': input := #29;
  75.           '0'..'9': input := chr(ord(c)-ord('0')+30);
  76.           #10: input := #40;
  77.           else input := '?'; (* denotes illegal character *)
  78.        end;
  79.      end
  80.   end(*input*);
  81. procedure unput(c : char);
  82.   begin
  83.     buf := c+buf
  84.   end(*unput*);
  85.  
  86. begin
  87.   buf := '';
  88.   if yylex=0 then (* done *)
  89. end.